<?php
class UploadFile{
	public $uploadUri = null;
	public $uploadType = 1;
	public $errMessage = null;
	public $type;
	public $owner_id;
	public $id_name;
	public function __construct($type,$owner_id){
		$this->uploadUri= Yii::app()->params['javaApiUrl'];
		$this->type = $type;
		$this->owner_id = $owner_id;
		
	}
	public function upload($file){
		if($this->type==0)
		{
			$this->id_name='userId';
		}
		if($this->type==1)
		{
			$this->id_name='placeId';
		}
		if($this->type==2)
		{
			$this->id_name='giftId';
		}
		$post_data = array (
				"packet" => '{"version":"1.0","cmd":"doCommonUpload","params":{"type":'.$this->type.',"'.$this->id_name.'":'.$this->owner_id.'},"token":"PHPWebServer"}',
				'packet_type'=>'json',
				// 要上传的本地文件地址
				"picfile" => "@$file",
		);
		
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->uploadUri);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		
		if(!$output){
			throw new Exception(curl_error($ch));
		}else{
			$result = json_decode($output,true);
			if($result){
				if($result['success']===true){
					return $result['result']['fileUrl'];
				}else{
					throw new Exception($result['text']);
				}
			}else{
				throw new Exception("json decode errror");
			}
		}
		curl_close($ch);
	}

}